package controllers.games;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.SwingWorker;

import models.games.MemoriaOrdinata;
import views.games.MemoriaOrdinataView;
import core.GameLauncher;
import core.GameLauncher.State;

public class MemoriaOrdinataController extends KeyAdapter implements
		ActionListener {

	private MemoriaOrdinata model;
	private MemoriaOrdinataView view;
	private int numbersToSort;
	private showAndHideNumbers shnNS = new showAndHideNumbers(
			Status.NEW_SEQUENCE);
	private showAndHideNumbers shnCS;
	private showAndHideNumbers shnW;
	// private int numRound;
	boolean flag = true;
	private GameLauncher gameLauncher;

	private int clickCount;

	// private boolean ignore = false;

	public MemoriaOrdinataController(MemoriaOrdinata model,
			MemoriaOrdinataView view, GameLauncher gameLauncher) {
		this.model = model;
		this.view = view;
		this.gameLauncher = gameLauncher;
		numbersToSort = model.getNumbersToSort();
		view.addActionListenerToGame(this);
		view.addKeyLiestenerToGame(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton source = (JButton) e.getSource();
		String inputText = source.getText();
		System.out.println(inputText);
		if (inputText.equals("Ricomincia")) {
			MemoriaOrdinata newModel = new MemoriaOrdinata(
					model.getCurrentUser());
			MemoriaOrdinataView newView = new MemoriaOrdinataView(
					newModel.getShuffledNumbers());
			MemoriaOrdinataController newController = new MemoriaOrdinataController(
					newModel, newView, gameLauncher);
			gameLauncher.setGame(newModel, newView, newController);
			gameLauncher.startTransition(State.GAME);
			model.addGameToDatabase();
			System.out.println("Ricomincia");
		}

		else if (inputText.equals("Menu Giochi")) {
			gameLauncher.startTransition(State.GAME_MENU);
			model.addGameToDatabase();
			System.out.println("Menu Giochi");
		}
		// else if(!ignore ){
		else {
			int input = Integer.parseInt(inputText);
			if (model.elaborateAnswer(input)) {
				view.setScore(model.getScore());
				source.setBackground(Color.WHITE);
				source.setForeground(Color.BLACK);
				clickCount++;
				if (clickCount == view.getDifficulty()) {
					// ignore = true;
					clickCount = 0;
					shnCS = new showAndHideNumbers(Status.CORRECT_SEQUENCE);
					shnCS.execute();

				}
				gameLauncher.correctAnswer();
			} else {
				clickCount = 0;
				source.setBackground(Color.RED);

				// ignore = true;

				shnW = new showAndHideNumbers(Status.WRONG);
				shnW.execute();
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			shnNS.execute();
		}
	}

	public enum Status {
		NEW_SEQUENCE, CORRECT_SEQUENCE, WRONG
	};

	class showAndHideNumbers extends SwingWorker<Void, Void> {
		private Status status;

		public showAndHideNumbers(Status status) {
			this.status = status;
		}

		@Override
		protected Void doInBackground() throws Exception {
			System.out.println(status + " in doInBackground");
			switch (status) {
			case NEW_SEQUENCE:
				// TimeUnit.SECONDS.sleep(1);
				view.showNumbers(); // mostra i numeri
				view.disableAll(); // li disabilita
				view.startTimer(); // avvia il time 3,2,1 nella view
				TimeUnit.SECONDS.sleep(3); // rimane in questo stato per 3
											// secondi -> Al termine chiama
											// done() e lo stato rimarrà BEGIN
				break;
			case CORRECT_SEQUENCE:
				view.colorCellsWin();
				view.showNumbers();
				view.disableAll();
				TimeUnit.SECONDS.sleep(1);
				break;
			case WRONG:
				view.showNumbers();
				view.disableAll();
				TimeUnit.SECONDS.sleep(1);
				break;
			}
			return null;
		}

		public void done() {
			System.out.println(status + " in done");
			switch (status) {
			case NEW_SEQUENCE:
				view.stopTimer();
				view.hideNumbers();
				view.enableAll();
				break;
			case CORRECT_SEQUENCE:
				view.refresh(model.getShuffledNumbers());
				shnNS = new showAndHideNumbers(Status.NEW_SEQUENCE);
				shnNS.execute();
				view.addActionListenerToGame(null);
				break;
			case WRONG:
				view.refresh(model.getShuffledNumbers());
				shnNS = new showAndHideNumbers(Status.NEW_SEQUENCE);
				shnNS.execute();
				view.addActionListenerToGame(null);
			}
			view.updateUI();
		}
	}// ShowAndHideNumbers

}
